package com.example.comm.test;



public class UDPHandlerTest {
    public static void main(String[] args) {

        String message = "这:,M,,*41\n" +
                "$GNGLL,3203.57069,N,1211.98789,E,8989\n" +
                "$BDGSV,3,2,11,13,45,242,42,20,21,193,30,32,62,134,29,36,17,257,42*6C\n" +
                "$BDGSV,3,3,11,38,72,267,22,46,09,210,35,59,,,33*67\n" +
                "$GNRMC,131350.000,A,3203.57071,N,12136.31184,E,0.00,0.00,270124,,,A*79\n" +
                "$GPGGA,235316.000,2959.9925,S,12000.0090,E,1,06,1.21,62.77,M,0.00,M,,*7B\n" +
                "$GNVTG,0.0";


        //接收后进行处理，解析出速度VTG，经纬度GLL、G
        double v;
        double lat_unfixed,lon_unfixed;   //未经处理的纬度，经度
        double altitude = -10000; //未经处理的海拔高度

        //VTG-----------------------
//        int findVTG = message.indexOf("VTG");
//        if(findVTG == -1) System.out.println("找不到字符串VTG");
//
//        int prov = message.indexOf("N",findVTG);
//        if(prov == -1) System.out.println("找不到提示字符N");
//
//        int aftv = message.indexOf(",K",findVTG);
//        if(aftv == -1) System.out.println("找不到提示字符K");
//
//        String vString = message.substring(prov+2 , aftv);
//        v = Double.parseDouble(vString);
//        System.out.println("小车速度为" + v);

        //GLL------------------------
        int findGLL = message.indexOf("GLL");
        if(findGLL == -1) System.out.println("找不到字符串VTG");

        //查找纬度和纬度方向NS
//        int findNS = message.indexOf("N",findGLL);
//        if(findNS == -1) {
//            findNS = message.indexOf("S",findGLL);
//            if(findNS == -1) System.out.println("找不到纬度方向NS");
//        }
//
//        String NS = message.substring(findNS,findNS+1);
//
//        String latString = message.substring(findGLL+4,findNS-1);
//        lat_unfixed = Double.parseDouble(latString);
//
//        //查找经度和经度方向EW
//        int findEW = message.indexOf("E",findGLL);
//        if(findEW == -1) {
//            findEW = message.indexOf("S",findGLL);
//            if(findEW == -1) System.out.println("找不到经度方向EW");
//        }
//
//        String EW = message.substring(findEW,findEW+1);
//
//        String lonString = message.substring(findNS+2,findEW-1);
//        lon_unfixed = Double.parseDouble(lonString);
//
//
//        System.out.println("处理前纬度为:"+lat_unfixed + "  纬度方向为:" + NS + "  处理前经度为:" + lon_unfixed + "经度方向为:" + EW);
//        System.out.println("处理前纬度为:"+lat_unfixed + "  纬度方向为:" + NS + "  处理前经度为:" + lon_unfixed + "经度方向为:" + EW);


        //GGA--------------------------
        int findGGA = message.indexOf("GGA");
        if(findGGA == -1) System.out.println("找不到字符串GGA");
        else {
            int aftalt = message.indexOf(",M",findGGA);
            if(aftalt == -1) System.out.println("找不到提示字符M");
            int proalt = 0;

            for(int i = aftalt - 1 ; i > 0; i --) {
                if(message.charAt(i) == ',') {
                    proalt = i;
                    break;
                }
            }

            if(proalt > 0 && aftalt > 0) {
                String altString = message.substring(proalt+1 , aftalt);
                altitude = Double.parseDouble(altString);
                System.out.println(altitude);
            }
        }



    }
}
